package com.tzbank.fingerPrint.esb.provide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.dc.eai.data.CompositeData;
import com.dcfs.esb.server.service.Service;
import com.tzbank.fingerPrint.entity.FingerPrintModel;
import com.tzbank.fingerPrint.service.FingerPrintService;
import com.tzbank.fingerPrint.utils.EsbConversionUtil;
import com.tzbank.fingerPrint.utils.GeneralSeqNo;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.SpringContextUtils;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class EsbIntoFingerPrintServiceImpl implements Service {

	static FingerPrintService fingerPrintService = SpringContextUtils.getBeanByName("fingerPrintService");

	/**
	 * 该类是发布在ESB在指纹服务的实现类 获取报文参数然后执行业务代码，之后再组装报文返回 详情如下：
	 * 
	 * @param data
	 * 
	 * @return 响应报文
	 */

	public CompositeData exec(CompositeData data) {
		log.info("==========进入FingerPrint_BIS服务========");
		CompositeData result = new CompositeData();
		// 系统头
		String serviceCode = (String) data.getStruct("SYS_HEAD").getField("SERVICE_CODE").getValue();// 服务码
		String CONSUMER_SEQ_NO = (String) data.getStruct("SYS_HEAD").getField("CONSUMER_SEQ_NO").getValue();// 系统流水
		String SERVICE_SCENE = (String) data.getStruct("SYS_HEAD").getField("SERVICE_SCENE").getValue();
		// 应用头
		String SeqNo = GeneralSeqNo.generalGlobalSeqNo("FRS");// 服务流水
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> publicMap = new HashMap<String, Object>();
		publicMap.put("CONSUMER_SEQ_NO", CONSUMER_SEQ_NO);// 是服务请求系统的流水号
		publicMap.put("ESB_SEQ_NO", "");// （ESB产生的流水号）为ESB系统的流水号

		publicMap.put("BUSS_SEQ_NO", "");//// 业务流水号）为应用头请求中BUSS_SEQ_NO
		publicMap.put("SERV_SEQ_NO", SeqNo);// 服务生成流水

		if ("03".equals(SERVICE_SCENE)) {// 指纹注册初始化(03)
			String auth_type = (String) data.getStruct("BODY").getField("AUTH_MODE").getValue();// 认证方式
			String ifaa_version = (String) data.getStruct("BODY").getField("IFAA_VERSION").getValue();// 指纹认证版本
			String requestSource = (String) data.getStruct("BODY").getField("SOURCE_CHANEL").getValue();// 请求来源
			String userId = (String) data.getStruct("BODY").getField("USER_NO").getValue();// 用户编号

			if (StringUtils.isEmpty(auth_type) || StringUtils.isEmpty(ifaa_version)) {
				result = toChang(new HashMap<String, Object>(), publicMap);
				log.info("==>>外部系统调用参数为空");
			} else {
				FingerPrintModel vo = new FingerPrintModel();
				vo.setAuth_type(auth_type);
				vo.setIfaa_version(ifaa_version);
				vo.setRequestSource(requestSource);
				vo.setUserId(userId);
				resultMap = fingerPrintService.fingerPrintApplyItialize(vo);
				result = toChang(resultMap, publicMap);
			}
		} else if ("04".equals(SERVICE_SCENE)) {// 指纹注册(04)

			String ifaf_message = (String) data.getStruct("BODY").getField("IFAF_MESSAGE").getValue();// 认证方式
			String ifaa_version = (String) data.getStruct("BODY").getField("IFAA_VERSION").getValue();// 指纹认证版本
			String requestSource = (String) data.getStruct("BODY").getField("SOURCE_CHANEL").getValue();// 请求来源
			String userId = (String) data.getStruct("BODY").getField("USER_NO").getValue();// 用户编号
			String out_biz_no = (String) data.getStruct("BODY").getField("VERIFY_CODE").getValue();// 认证码

			if (StringUtils.isEmpty(ifaf_message) || StringUtils.isEmpty(out_biz_no)) {
				result = toChang(new HashMap<String, Object>(), publicMap);
				log.info("==>>外部系统调用参数为空");
			} else {
				FingerPrintModel vo = new FingerPrintModel();
				vo.setIfaf_message(ifaf_message);
				vo.setIfaa_version(ifaa_version);
				vo.setOut_biz_no(out_biz_no);
				vo.setRequestSource(requestSource);
				vo.setUserId(userId);
				resultMap = fingerPrintService.fingerPrintApply(vo);
				result = toChang(resultMap, publicMap);
			}

		} else if ("07".equals(SERVICE_SCENE)) {// 指纹校验初始化(07)

			String ai_token = (String) data.getStruct("BODY").getField("TOKEN_INFO").getValue();// TOKEN信息
			String ifaa_version = (String) data.getStruct("BODY").getField("IFAA_VERSION").getValue();// 指纹认证版本
			String requestSource = (String) data.getStruct("BODY").getField("SOURCE_CHANEL").getValue();// 请求来源
			String userId = (String) data.getStruct("BODY").getField("USER_NO").getValue();// 用户编号

			if (StringUtils.isEmpty(ifaa_version) || StringUtils.isEmpty(ai_token)) {
				result = toChang(new HashMap<String, Object>(), publicMap);
				log.info("==>>外部系统调用参数为空");
			} else {
				FingerPrintModel vo = new FingerPrintModel();
				vo.setAi_token(ai_token);
				vo.setIfaa_version(ifaa_version);
				vo.setRequestSource(requestSource);
				vo.setUserId(userId);
				resultMap = fingerPrintService.fingerPrintVerifyInitialize(vo);
				result = toChang(resultMap, publicMap);
			}

		} else if ("05".equals(SERVICE_SCENE)) {// 指纹校验(05)

			String ifaf_message = (String) data.getStruct("BODY").getField("IFAF_MESSAGE").getValue();// TOKEN信息
			String ifaa_version = (String) data.getStruct("BODY").getField("IFAA_VERSION").getValue();// 指纹认证版本
			String requestSource = (String) data.getStruct("BODY").getField("SOURCE_CHANEL").getValue();// 请求来源
			String userId = (String) data.getStruct("BODY").getField("USER_NO").getValue();// 用户编号
			String out_biz_no = (String) data.getStruct("BODY").getField("VERIFY_CODE").getValue();// 认证码
			String needAuthData = (String) data.getStruct("BODY").getField("VERIFY_FLAG").getValue();// 认证标识
			String subAction = (String) data.getStruct("BODY").getField("EXE_MODE").getValue();// 执行方式

			if (StringUtils.isEmpty(ifaa_version) || StringUtils.isEmpty(out_biz_no)) {
				result = toChang(new HashMap<String, Object>(), publicMap);
				log.info("==>>外部系统调用参数为空");
			} else {
				FingerPrintModel vo = new FingerPrintModel();
				vo.setIfaf_message(ifaf_message);
				vo.setOut_biz_no(out_biz_no);
				vo.setSubAction(subAction);
				vo.setNeedAuthData(Boolean.getBoolean(needAuthData));
				vo.setIfaa_version(ifaa_version);
				vo.setRequestSource(requestSource);
				vo.setUserId(userId);
				resultMap = fingerPrintService.fingerPrintVerifyInitialize(vo);
				result = toChang(resultMap, publicMap);
			}

		} else if ("06".equals(SERVICE_SCENE)) {// 指纹注销(06)

			String ai_token = (String) data.getStruct("BODY").getField("TOKEN_INFO").getValue();// TOKEN信息
			String ifaa_version = (String) data.getStruct("BODY").getField("IFAA_VERSION").getValue();// 指纹认证版本
			String requestSource = (String) data.getStruct("BODY").getField("SOURCE_CHANEL").getValue();// 请求来源
			String userId = (String) data.getStruct("BODY").getField("USER_NO").getValue();// 用户编号

			if (StringUtils.isEmpty(ifaa_version) || StringUtils.isEmpty(ai_token)) {
				result = toChang(new HashMap<String, Object>(), publicMap);
				log.info("==>>外部系统调用参数为空");
			} else {
				FingerPrintModel vo = new FingerPrintModel();
				vo.setAi_token(ai_token);
				vo.setIfaa_version(ifaa_version);
				vo.setIfaa_version(ifaa_version);
				vo.setRequestSource(requestSource);
				vo.setUserId(userId);
				resultMap = fingerPrintService.fingerPrintVerifyInitialize(vo);
				result = toChang(resultMap, publicMap);
			}

		} else {
			result = toChang(new HashMap<String, Object>(), publicMap);
			log.info("==>>外部系统调用参数为空");
		}

		log.info("调用FingerPrint结束>>服务流水" + SeqNo + "服务码：" + SERVICE_SCENE);
		return result;
	}

	/**
	 * 将返回结果封装
	 * 
	 * @param resultMap
	 * @param publicMap
	 * @return
	 */
	public CompositeData toChang(Map<String, Object> resultMap, Map<String, Object> publicMap) {

		CompositeData result = new CompositeData();
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = new HashMap<String, Object>();
		Map<String, Object> reqAppHead = new HashMap<String, Object>();
		List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>();
		Map<String, Object> retCode = new HashMap<String, Object>();
		reqSysHead.put("consumerSeqNo", publicMap.get("CONSUMER_SEQ_NO")); // 是服务请求系统的流水号
		reqSysHead.put("esbSeqNo", publicMap.get("ESB_SEQ_NO")); //（ESB产生的流水号）为ESB系统的流水号
		//应用头应答数据
		reqAppHead.put("bussSeqNo", publicMap.get("BUSS_SEQ_NO")); //业务流水号）为应用头请求中BUSS_SEQ_NO
		reqAppHead.put("servSeqNo", publicMap.get("SERV_SEQ_NO"));//服务流水
		inputMap.put("appHead", reqAppHead);
		if (resultMap.isEmpty()) {
			retCode.put("retMsg", "参数错误");
			retCode.put("retCode", "000000");
			ret.add(retCode);
			reqSysHead.put("ret", ret);
			reqSysHead.put("retStatus", "F");

		} else if (CodeUtil.CODE_200.getCode().equals(resultMap.get(resultMap.get("code")))) {// 结果成功获取
			//
			retCode.put("retMsg", "成功");
			retCode.put("retCode", CodeUtil.CODE_200.getCode());
			ret.add(retCode);
			reqSysHead.put("ret", ret);
			reqSysHead.put("retStatus", "S");

		} else {// 程序异常
				//
			retCode.put("retMsg", resultMap.get("msg"));
			retCode.put("retCode", resultMap.get("code"));
			ret.add(retCode);
			reqSysHead.put("ret", ret);
			reqSysHead.put("retStatus", "F");

		}
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("data", resultMap.get("data"));// y
		// Map转换Esb报文
		result = EsbConversionUtil.mapToCompositeData(inputMap);
		return result;
	}

}
